const crypto = require('crypto');

const key = crypto.randomBytes(32);
const iv = Buffer.from(crypto.randomBytes(16))
// 对称加密
// const cipher = crypto.createCipheriv('aes-256-cbc',key,iv)
// // 加密
// cipher.update('lzy','utf-8','hex')
// const result =  cipher.final('hex')
// console.log(result,'result');
// // 解密
// const de = crypto.createDecipheriv('aes-256-cbc', key,iv);
// const name = de.update(result,'hex','utf-8')
// console.log(de.update(result,'hex','utf-8'));


// 非对称加密
// privateKey公钥  publicKey
const {privateKey, publicKey} = crypto.generateKeyPairSync('rsa', {
    modulusLength:2048 //越长越安全 越慢
})
const encryupted = crypto.publicEncrypt(publicKey, Buffer.from('lzy'));
console.log(encryupted.toString('hex'));
const decrypted = crypto.privateDecrypt(privateKey,encryupted);
console.log(decrypted.toString());